package com.pureut.storage.controller;

import com.pureut.storage.dto.PickingRestDbDto;
import com.pureut.storage.dto.SubcontractReceiptDto;
import com.pureut.storage.export.SubcontractPurchaseReceiptExport;
import com.pureut.storage.service.SubcontractReceiptService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.*;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/***
 * 委外入库控制层
 * Author:C
 * Date:2022/12/27 16:37
 * @DESC
 */
@RestController
@RequestMapping("/subcontract-receipt")
public class SubcontractReceiptController extends BaseController {

    @Resource
    SubcontractReceiptService subcontractReceiptService;


    /**
     * 获取委外入库分页列表
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:list")
    @GetMapping("/list")
    public TableDataInfo<SubcontractReceiptDto> getList(SubcontractReceiptVo subcontractReceiptVo, PageQuery pageQuery) throws Exception {
        return subcontractReceiptService.getReceiptList(subcontractReceiptVo,pageQuery);
    }

    /**
     * 新增委外入库
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:add")
    @Log(title = "新增委外入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody SubcontractReceiptVo subcontractReceiptVo) throws Exception {
        return toAjax(subcontractReceiptService.addSubcontractReceipt(subcontractReceiptVo));
    }

    /**
     * 新增委外入库（自动新增 其他模块插入数据时使用）
     */
    @Log(title = "新增委外入库", businessType = BusinessType.INSERT)
    @PostMapping("/addOther")
    public R<Void> addOther(@RequestBody SubcontractReceiptOtherVo subcontractReceiptOtherVo) throws Exception {
        return toAjax(subcontractReceiptService.addOtherSubcontractReceipt(subcontractReceiptOtherVo));
    }

    /**
     * 关结委外入库
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:closing")
    @Log(title = "关结委外入库", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public R<Void> close(@RequestBody SubcontractReceiptVo subcontractReceiptVo) {
        return toAjax(subcontractReceiptService.close(subcontractReceiptVo));
    }

    /**
     * 查看
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:query")
    @GetMapping("/view/{id}")
    public R<SubcontractReceiptDto> view(@PathVariable("id") Long id) {
        return R.ok(subcontractReceiptService.getView(id));
    }

    /**
     * 修改委外入库
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:edit")
    @Log(title = "修改委外入库", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R<Void> updateSubcontractReceipt(@Validated @RequestBody SubcontractReceiptVo subcontractReceiptVo) {
        return toAjax(subcontractReceiptService.updateSubcontractReceipt(subcontractReceiptVo));
    }

    /**
     * 删除委外入库
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:remove")
    @Log(title = "删除委外入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{idStr}")
    public R<Void> deleteSubcontractReceipt(@PathVariable("idStr") String idStr) {
        return toAjax(subcontractReceiptService.deleteSubcontractReceipt(idStr));
    }

    /**
     * 入库打印
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:deliver")
    @Log(title = "入库打印", businessType = BusinessType.INSERT)
    @PostMapping("/warehousInspection")
    public R<Void> warehousInspection(@RequestBody WarehousInspectionVo warehousInspectionVo) throws Exception {
        return toAjax(subcontractReceiptService.subcontractWarehousInspection(warehousInspectionVo));
    }

    /**
     * 导出
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:export")
    @PostMapping("/subcontractMessageExport")
    public R<Void> subcontractMessageExport(HttpServletResponse response, SubcontractReceiptVo subcontractReceiptVo) throws IOException {
        List<SubcontractPurchaseReceiptExport> infoExport = subcontractReceiptService.getInfoExport(subcontractReceiptVo);
        ExcelUtil<SubcontractPurchaseReceiptExport> excelUtil = new ExcelUtil<SubcontractPurchaseReceiptExport>(SubcontractPurchaseReceiptExport.class);
        excelUtil.excelWriter(response, infoExport, "委外入库信息.xlsx");
        return R.ok();
    }

    /**
     * 委外入库（pda）
     */
//    @RequiresPermissions("warehouse:warehousing:subcontracting:warehousing")
    @PostMapping("/subcontractPDA")
    @Log(title = "采购入库(pda)", businessType = BusinessType.UPDATE)
    public R<PickingRestDbDto> subcontractPda(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return R.ok(subcontractReceiptService.subcontractPda(pickingRestDbVo));
    }

    /**
     * 委外入库状态及数据修改（委外订单关结时 fegin调用）
     */
    @PostMapping("/updateSubcontractReceiptStatus")
    public R<Void> updateSubcontractReceiptStatus(@RequestBody UpdateReceiptStatusVo updateReceiptStatusVo) {
        return toAjax(subcontractReceiptService.updateSubcontractReceiptStatus(updateReceiptStatusVo));
    }
}
